package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.ship.info.SupplierSystemOrderShipInfoDTO;
import com.ctshk.rpc.booking.dto.system.ship.info.SupplierSystemOrderShipTicketInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderShip;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderShipTicket;
import com.ctshk.rpc.booking.mapper.*;
import com.ctshk.rpc.booking.req.system.ship.add.SupplierSystemOrderShipAddReq;
import com.ctshk.rpc.booking.req.system.ship.add.SupplierSystemOrderShipTicketAddReq;
import com.ctshk.rpc.booking.req.system.ship.edit.SupplierSystemOrderShipEditReq;
import com.ctshk.rpc.booking.req.system.ship.edit.SupplierSystemOrderShipTicketEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderShipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统船票订单 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierSystemOrderShipServiceImpl implements ISupplierSystemOrderShipService {
    @Autowired
    SupplierSystemOrderShipMapper supplierSystemOrderShipMapper;
    @Autowired
    SupplierSystemOrderShipTicketMapper supplierSystemOrderShipTicketMapper;

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, SupplierSystemOrderShipAddReq supplierSystemOrderShipAddReq) {
        if (supplierSystemOrderShipAddReq != null) {
            SupplierSystemOrderShip supplierSystemOrderShip = EntityUtil.copy(supplierSystemOrderShipAddReq, SupplierSystemOrderShip.class);
            supplierSystemOrderShip.setId(SnowflakeIdWorker.nextId());
            supplierSystemOrderShip.setOrderId(id);
            supplierSystemOrderShip.setCreateId(userId);
            supplierSystemOrderShip.setGmtCreate(nowDateTime);
            supplierSystemOrderShip.setModifiedId(userId);
            supplierSystemOrderShip.setGmtModified(nowDateTime);
            supplierSystemOrderShip.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierSystemOrderShipMapper.insert(supplierSystemOrderShip);
            //添加旅客
            List<SupplierSystemOrderShipTicketAddReq> supplierSystemOrderShipTicketAddReqList = supplierSystemOrderShipAddReq.getSupplierSystemOrderShipTicketAddReqList();
            if (CollectionUtils.isNotEmpty(supplierSystemOrderShipTicketAddReqList)) {
                supplierSystemOrderShipTicketAddReqList.forEach(shipTicketAdd -> {
                    SupplierSystemOrderShipTicket supplierSystemOrderShipTicket = EntityUtil.copy(shipTicketAdd, SupplierSystemOrderShipTicket.class);
                    supplierSystemOrderShipTicket.setId(SnowflakeIdWorker.nextId());
                    supplierSystemOrderShipTicket.setOrderId(id);
                    supplierSystemOrderShipTicket.setShipId(supplierSystemOrderShip.getId());
                    supplierSystemOrderShipTicket.setCreateId(userId);
                    supplierSystemOrderShipTicket.setGmtCreate(nowDateTime);
                    supplierSystemOrderShipTicket.setModifiedId(userId);
                    supplierSystemOrderShipTicket.setGmtModified(nowDateTime);
                    supplierSystemOrderShipTicket.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierSystemOrderShipTicketMapper.insert(supplierSystemOrderShipTicket);
                });
            }
        }
    }

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, SupplierSystemOrderShipEditReq supplierSystemOrderShipEditReq) {
        if (supplierSystemOrderShipEditReq != null) {
            UpdateWrapper<SupplierSystemOrderShip> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(SupplierSystemOrderShip::getId, supplierSystemOrderShipEditReq.getId());
            updateWrapper.lambda().set(SupplierSystemOrderShip::getCustomerRemark, supplierSystemOrderShipEditReq.getCustomerRemark());
            updateWrapper.lambda().set(SupplierSystemOrderShip::getSupplierRemark, supplierSystemOrderShipEditReq.getSupplierRemark());
            updateWrapper.lambda().set(SupplierSystemOrderShip::getModifiedId, userId);
            updateWrapper.lambda().set(SupplierSystemOrderShip::getGmtModified, nowDateTime);
            supplierSystemOrderShipMapper.update(new SupplierSystemOrderShip(), updateWrapper);
            List<SupplierSystemOrderShipTicketEditReq> supplierSystemOrderShipTicketEditReqList = supplierSystemOrderShipEditReq.getSupplierSystemOrderShipTicketEditReqList();
            if(CollectionUtils.isNotEmpty(supplierSystemOrderShipTicketEditReqList)){
                supplierSystemOrderShipTicketEditReqList.forEach(shipTicketEditReq -> {
                    UpdateWrapper<SupplierSystemOrderShipTicket> ticketUpdateWrapper = new UpdateWrapper<>();
                    ticketUpdateWrapper.lambda().eq(SupplierSystemOrderShipTicket::getId, shipTicketEditReq.getId());
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderShipTicket::getSinglePrice, shipTicketEditReq.getSinglePrice());
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderShipTicket::getTaxAmount, shipTicketEditReq.getTaxAmount());
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderShipTicket::getModifiedId, userId);
                    ticketUpdateWrapper.lambda().set(SupplierSystemOrderShipTicket::getGmtModified, nowDateTime);
                    supplierSystemOrderShipTicketMapper.update(new SupplierSystemOrderShipTicket(), ticketUpdateWrapper);
                });
            }
        }
    }


    @Override
    public List<SupplierSystemOrderShipInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderShip> shipQueryWrapper = new QueryWrapper<>();
        shipQueryWrapper.lambda().eq(SupplierSystemOrderShip::getOrderId, id);
        List<SupplierSystemOrderShip> supplierSystemOrderShipList = supplierSystemOrderShipMapper.selectList(shipQueryWrapper);
        List<SupplierSystemOrderShipInfoDTO> supplierSystemOrderShipInfoDTOList = EntityUtil.copyList(supplierSystemOrderShipList, SupplierSystemOrderShipInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderShipInfoDTOList)) {
            supplierSystemOrderShipInfoDTOList.forEach(shipCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderShipTicket> shipTicketQueryWrapper = new QueryWrapper<>();
                shipTicketQueryWrapper.lambda().eq(SupplierSystemOrderShipTicket::getOrderId, id);
                List<SupplierSystemOrderShipTicket> supplierSystemOrderShipTickets = supplierSystemOrderShipTicketMapper.selectList(shipTicketQueryWrapper);
                List<SupplierSystemOrderShipTicketInfoDTO> supplierSystemOrderShipTicketInfoDTOS = EntityUtil.copyList(supplierSystemOrderShipTickets, SupplierSystemOrderShipTicketInfoDTO.class);
                shipCheckinDTO.setSupplierSystemOrderShipTicketInfoDTOList(supplierSystemOrderShipTicketInfoDTOS);
            });
        }
        return supplierSystemOrderShipInfoDTOList;
    }
}
